Skip to content

FOUR-34499: Improve performance - #9186

Open
caleeli wants to merge 5 commits into
developfrom
epic/FOUR-33902-2617
Open

caleeli wants to merge 5 commits into
developfrom
epic/FOUR-33902-2617

Conversation

@caleeli

@caleeli caleeli commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

[Perf 2026.17] Review the perfomance across all the plataform

Related Tickets & Packages

Code Review Checklist

  • I have pulled this code locally and tested it on my instance, along with any associated packages.
  • This code adheres to ProcessMaker Coding Guidelines.
  • This code includes a unit test or an E2E test that tests its functionality, or is covered by an existing test.
  • This solution fixes the bug reported in the original ticket.
  • This solution does not alter the expected output of a component in a way that would break existing Processes.
  • This solution does not implement any breaking changes that would invalidate documentation or cause existing Processes to fail.
  • This solution has been tested with enterprise packages that rely on its functionality and does not introduce bugs in those packages.
  • This code does not duplicate functionality that already exists in the framework or in ProcessMaker.
  • This ticket conforms to the PRD associated with this part of ProcessMaker.

ci:deploy
ci:run-testbench

.

@cursor

cursor Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes core multi-tenant settings read/write paths and cross-worker cache coherence; mis-epoch or envelope handling could serve stale config or extra DB load, though Redis failures are designed to fall back to the database.

Overview
Adds a process-local (L1) settings cache on top of the existing Redis settings store, coordinated across Octane/FPM/queue workers with a shared Redis epoch so workers drop stale in-memory maps after publishes.

SettingCacheManager now checks L1, then Redis (with envelopes so cached null is not treated as a miss), and exposes resolve() with epoch fencing to avoid publishing pre-commit loads. Writes (set/delete/clear/clearBy) and publishEpoch() advance the epoch; if the epoch cannot be read, reads fail closed to the DB.

Setting::byKey() uses resolve() when backed by SettingCacheManager. SettingObserver defers epoch publication to saved() (after commit) instead of only invalidating on save, and supports optional bump_epoch on hard deletes.

ResetRequestState and Queue::before call SettingMemoryCache::beginRequest() so epoch is re-checked once per HTTP request or job without unnecessary L1 clears. New SettingMemoryCacheTest (and test setup tweaks) cover L1 hits, epoch bumps, tenants, races, and jobs.

Reviewed by Cursor Bugbot for commit 0f640d0. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@cursor
cursor Bot requested review from nolanpro and rodriquelca November 19, 2026 20:11
gproly

This comment was marked as resolved.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

}
} else {
$this->writeStoreEntry($key, $value);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale resolve can overwrite Redis

High Severity

resolve() re-checks the epoch before writing Redis, but the write itself is not guarded. A concurrent saved()/set() can bump the epoch and publish a committed value in the gap between forceResyncEpoch() and writeStoreEntry(), so a loader that still holds a pre-commit row overwrites L2 under the new epoch. Later workers then hit that stale envelope and never go back to the database until the setting is saved again.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 13d40b1. Configure here.

if (method_exists($settingCache, 'publishEpoch')) {
$settingCache->publishEpoch();
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Epoch publish runs before commit

Medium Severity

The new saved() hook comments that it publishes the L1 epoch after the row is committed, but it listens to Eloquent saved without afterCommit. Inside a wrapping DB::transaction(), that still runs before commit, so concurrent byKey() loaders can refill Redis with the pre-commit row after the epoch bump.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 13d40b1. Configure here.

self::$localEpochs[$tenantKey] = $newEpoch;
self::$epochUnreliable[$tenantKey] = false;
self::$syncedThisRequest[$tenantKey] = true;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed epoch bump marked reliable

Medium Severity

bumpEpoch() always sets epochUnreliable to false and syncedThisRequest to true, even when incrementRemoteEpoch() fails both INCR and SET. The publishing worker then treats L1/L2 as trustworthy although other workers never saw an epoch change and keep serving stale memory.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 13d40b1. Configure here.

@caleeli
caleeli requested a review from gproly November 20, 2026 20:36
@pmPaulis pmPaulis changed the title Improve performance FOUR-34499: Improve performance Nov 23, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

public static function put(string $key, mixed $value): void
{
$tenantKey = self::currentTenantKey();
self::$items[$tenantKey][$key] = $value;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L1 cache shares mutable setting models

Medium Severity

Setting::byKey() now returns the live L1 object rather than a deserialized copy. In-place changes to that model or its nested config object stay in the worker map and are reused on later byKey() calls until an epoch bump. Callers such as CSS override and settings upload mutate the instance from byKey() before save().

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 284c671. Configure here.

@nolanpro

Copy link
Copy Markdown
Contributor

QA server K8S was successfully deployed https://tenant-1.ci-8c8b0bf840.engk8s.processmaker.net

@decisions-sonarqube

Copy link
Copy Markdown

Quality Gate failed Quality Gate failed

Failed conditions
4 New issues
4 New Maintainability Issues (required ≤ 0)

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE SonarQube for IDE

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

There are 5 total unresolved issues (including 4 from previous reviews).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0f640d0. Configure here.

}

$value = $this->loadAndPublish($key, $loader, $epochAtStart);
SettingMemoryCache::put($key, $value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreliable resolve fills worker L1

Medium Severity

When the epoch read after the loader fails, loadAndPublish returns without fencing, but resolveCacheMiss still puts that value into L1. The next request on the same worker keeps that map if the epoch is unchanged, so a stale or pre-commit setting can be served from memory instead of Redis or the database.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0f640d0. Configure here.

return $value;
}

if (SettingMemoryCache::localEpoch() !== $epochAtStart) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review

Severity: MEDIUM

Description: SettingObserver::saved() drops the Redis settings key and publishEpoch() inside an open outer transaction. The observer does not use afterCommit, so Eloquent saved runs when the model is written, before DB::transaction commits. A concurrent worker that then resolves that key sees the new epoch, misses Redis, and loadAndPublish() stores loader()'s SELECT. On the default MySQL InnoDB connection that SELECT cannot see the writer's uncommitted row, so writeStoreEntry() persists the previous row with no TTL under the new epoch. DevLinkInstall keeps the transaction open across Bundle::installSettingsPayloads(), which updateOrCreate()s Log-In & Auth settings and never calls SettingCacheManager::set() after the outer commit. RefreshArtisanCaches::handle() is empty, so the terminating refresh does not remove the refilled key. Later Setting::byKey and SettingsConfigRepository reads keep serving the previous password-policy, 2FA, SSO, and session-control values. SettingController::update() does call set() with the refreshed model after save(), so that direct API write publishes the committed row.

Impact: A committed bundle install can leave 2FA, password policy, SSO/login, and session IP/device restrictions at the previous value for every worker until some later epoch bump. Login and session middleware keep enforcing that stale policy. The refill is any overlapping settings read during the install transaction; the poisoned Redis entry then applies to unauthenticated login traffic. This needs an in-flight privileged install, so it is not a scriptable unauthenticated attack on its own, and it does not grant full control of the app.

Remediation: Run epoch publish and the Redis write from DB::afterCommit (observer $afterCommit / ShouldHandleEventsAfterCommit, or an explicit afterCommit callback), and only after the committed value is stored. In loadAndPublish(), when the epoch moved or the key is missing after a bump, do not writeStoreEntry() the loader result; return it for this call and leave Redis empty so a pre-commit snapshot cannot become the authoritative entry. Bundle and DevLink setting writes should set() the committed model in that after-commit hook.

return $value;
}

if (SettingMemoryCache::localEpoch() !== $epochAtStart) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review

Severity: MEDIUM

Description: loadAndPublish() can publish a pre-update Setting row to Redis after the shared epoch has already moved, so other workers treat the old password-policy, 2FA, session-control, or default-SSO value as current.

Impact: Login and session requests call Setting::byKey/config() with no credentials (login throttling, 2FA, sso.default.login, session-control.ip_restriction/device_restriction). A fill that lands after saved()/set() stamps the previous row at the new epoch. The next worker syncs to that epoch, hits Redis, and enforces the old policy with no DB read until some later epoch bump. The saving worker still keeps the new value in its own L1 after set(), so the admin node looks updated while the others do not. The wide window is an outer transaction: SettingObserver::saved() forgets the key and bumps the epoch before commit and is not afterCommit, and DevLink install of Log-In & Auth settings never calls set() after commit, so any overlapping byKey writes the snapshot that other connections cannot see yet. The narrow window is set() itself: the epoch GET and the later SET are separate Redis commands, so a resolver that still observed the old epoch can overwrite the committed payload after bumpEpoch().

Remediation: Write a fill only with a Redis compare-and-set on the epoch observed before loader() (Lua or WATCH/MULTI). If the epoch changed, do not writeStoreEntry(); return a store hit when one exists, otherwise return loader() to this caller only. Bump and publish only a value read after the commit that produced the bump. Set SettingObserver::$afterCommit = true (or publish the epoch from DB::afterCommit) so saved() does not invalidate inside an open transaction.

/**
* Atomically increment the shared epoch; on failure, seed with $fallback.
*/
public static function incrementOrFallback(int $fallback): int

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review

Severity: MEDIUM

Description: SettingEpochRedis::incrementOrFallback() treats a failed epoch INCR as a successful publish: it logs, may SET the caller's localEpoch+1 over the shared key, and always returns that fallback. SettingMemoryCache::bumpEpoch() then clears only the publishing worker's L1 and records epochUnreliable=false and syncedThisRequest=true. Peer workers invalidate solely by observing a higher Redis epoch, so an unchanged or rewound epoch leaves their existing L1 entries in place. resolve() then returns those entries and does not hit Redis or the database.

Impact: The shared epoch is the only cross-worker freshness gate for process-local settings L1. After a committed setting save, a publisher-side INCR failure (timeout, connection reset, OOM, or READONLY on the epoch write) while peer GETs still succeed leaves other Octane or queue workers enforcing the previous value. Session IP and device restrictions are read through Setting::configByKey on each login check, and SSO default login is read through Setting::byKey, so those workers can keep allowing sessions the new policy was meant to block. A fallback SET can also move the shared counter backward when the publisher's localEpoch is behind, so workers already sitting on that lower value never invalidate. A total epoch GET failure still fail-closes the worker that sees it; this gap is the swallowed write path, and it is not attacker-scriptable.

Remediation: Do not return or SET a locally computed epoch when INCR fails. Leave the publisher epochUnreliable so its own later reads skip L1, and do not mark syncedThisRequest. Only advance the shared counter with INCR (or a compare-and-set that refuses a lower value). Peers should drop L1 unless a successful GET shows an epoch greater than the one they cached with the entry.

@pmPaulis
pmPaulis removed the request for review from rodriquelca November 24, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants